package com.example.keli.ui.userinfo;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.keli.R;
import com.example.keli.api.RetrofitManager;
import com.example.keli.base.BaseDialog;
import com.example.keli.base.adapter.RecyclerAdapter;
import com.example.keli.base.utils.DensityUtil;
import com.example.keli.base.widget.RoundWhiteImageView;
import com.example.keli.ui.home.activity.ObjectDetailActivity;
import com.example.keli.ui.my.model.UserInfo;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;

/**
 * description $desc$
 * created by jerry on 2019/3/21.
 */
public class UserInfoDialog extends BaseDialog {

    private static final String TAG = "UserInfoDialog";
    @BindView(R.id.iv_avatar)
    RoundWhiteImageView ivAvatar;
    @BindView(R.id.tv_nick)
    TextView tvNick;
    @BindView(R.id.tv_age)
    TextView tvAge;
    @BindView(R.id.iv_sex)
    ImageView ivSex;
    @BindView(R.id.tv_ticketNum)
    TextView tvTicketNum;
    @BindView(R.id.tv_likeNum)
    TextView tvLikeNum;
    @BindView(R.id.tv_giftNum)
    TextView tvGiftNum;
    @BindView(R.id.recycler_active)
    RecyclerView recyclerActive;
    @BindView(R.id.recycler_active_all)
    RecyclerView recyclerActiveAll;
    @BindView(R.id.ll_basic)
    LinearLayout llBasic;
    @BindView(R.id.ll_more)
    LinearLayout llMore;
    @BindView(R.id.tv_viewAll)
    TextView tvViewAll;

    private UserInfoActiveAdapter mAdapter;
    private UserInfoActiveAdapter mAdapterAll;
    private List<UserInfo.ActivityListBean.DataBean> mActives;//最近参加的活动
    private int mUid = 1;//用户uid
    private boolean isViewAll = false;


    public UserInfoDialog(@NonNull Context context) {
        super(context);
    }

    public UserInfoDialog(@NonNull Context context, int uid) {
        super(context, R.style.DialogStyle);
        this.mUid = uid;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.dialog_userinfo;
    }

    @Override
    protected void onInitView() {
        mActives = new ArrayList<>();
    }

    @Override
    protected void onInitData() {
        Map<String, String> map = new HashMap<>();
        map.put("uid", String.valueOf(mUid));
        map.put("page", String.valueOf(1));
        RetrofitManager.ApiFunc(RetrofitManager.mApiService.getUser(map), new SubscriberListener<BaseResponse<UserInfo>>() {
            @Override
            public void onSuccess(BaseResponse<UserInfo> listBaseResponse) {
                UserInfo userInfo = listBaseResponse.data;
                if (userInfo != null) {
                    Glide.with(getContext()).load(userInfo.getAvatar()).into(ivAvatar);
                    tvNick.setText(userInfo.getNickname());
                    //1男2女
                    if ("1".equals(userInfo.getSex())){
                        ivSex.setImageResource(R.mipmap.female_icon);
                    }else {
                        ivSex.setImageResource(R.mipmap.male_icon);
                    }
                    tvAge.setText(String.valueOf(userInfo.getAge()));
                    tvTicketNum.setText(String.valueOf(userInfo.getTickets_num()));
                    tvGiftNum.setText(String.valueOf(userInfo.getLikes_num()));
                    tvLikeNum.setText(String.valueOf(userInfo.getLove_num()));
                    mActives = userInfo.getActivityList().getData();
                    initRecycleAll();
                    initRecycle();
                }
            }

            @Override
            public void onFailer(String msg) {
            }

            @Override
            public void onTokenError() {
            }
        });
    }

    private void initRecycle() {
        //初始化最近
        List<UserInfo.ActivityListBean.DataBean> actives = new ArrayList<>();
        for (int i=0; i<mActives.size(); i++){
            if (i<3){
                actives.add(mActives.get(i));
            }
        }
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerActive.setLayoutManager(linearLayoutManager);
        mAdapter = new UserInfoActiveAdapter(getContext(), R.layout.item_userinfo_active, actives);
        recyclerActive.setAdapter(mAdapter);
        setDivier(recyclerActive);
        mAdapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, View view, Object o, int position) {
                UserInfo.ActivityListBean.DataBean dataBean = (UserInfo.ActivityListBean.DataBean) o;
                if (dataBean != null){
                    String uid = String.valueOf(dataBean.getUid());
                    String aaid = String.valueOf(dataBean.getAaid());
                    ObjectDetailActivity.start(mActivity, uid, aaid);
                    dismiss();
                }
            }
        });
    }

    private void setDivier(RecyclerView recyclerView) {
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mActivity)
                .color(ContextCompat.getColor(mActivity, R.color.divider_line))
                .size(DensityUtil.dip2px(mActivity, 1))
                .build());
    }

    private void initRecycleAll() {
        //初始化全部
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerActiveAll.setLayoutManager(linearLayoutManager);
        mAdapterAll = new UserInfoActiveAdapter(getContext(), R.layout.item_userinfo_active, mActives);
        recyclerActiveAll.setAdapter(mAdapterAll);
        setDivier(recyclerActiveAll);
        mAdapterAll.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, View view, Object o, int position) {
                UserInfo.ActivityListBean.DataBean dataBean = (UserInfo.ActivityListBean.DataBean) o;
                if (dataBean != null){
                    String uid = String.valueOf(dataBean.getUid());
                    String aaid = String.valueOf(dataBean.getAaid());
                    ObjectDetailActivity.start(mActivity, uid, aaid);
                    dismiss();
                }
            }
        });
    }

    @OnClick(R.id.tv_viewAll)
    public void onViewClicked() {
        isViewAll = !isViewAll;
        if (isViewAll) {
            //显示全部
            tvViewAll.setText("收起");
            llBasic.setVisibility(View.GONE);
            llMore.setVisibility(View.VISIBLE);
        } else {
            tvViewAll.setText("查看全部");
            llBasic.setVisibility(View.VISIBLE);
            llMore.setVisibility(View.GONE);
        }
    }

    @OnClick(R.id.rootView)
    public void onRootViewClicked() {
        dismiss();
    }
}
